<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */

define('IN_ADMIN_COMPONENT_CONTROLLER', 'true');

class ProductOptionsComponent
{
	function ProductOptionsComponent()
	{
	}

	function action(&$params, $action)
	{
		if ($action == 'Update') 
			return $this->edit($params);
		else
			return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$languageId = (int)$params['languageId'];

		$paginate = (boolean)$params['paginate'];
		$max_rows = (int)$params['max_rows'];
		$offset = (int)$params['offset'];

		$sqlQuery = 'select po.products_options_id, po.products_options_name ' .
		'from ' . TABLE_PRODUCTS_OPTIONS . ' po ' .
		'where po.language_id=' . $languageId . ' ' .
		'order by po.products_options_id';
		if ($paginate) {
			$sqlQuery .= ' limit ' . $offset . ', ' . $max_rows;
		}
		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
		$i = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$value{$handle}{$i}{'id'}= (int)$row['products_options_id'];
			$value{$handle}{$i}{'name'}= $row['products_options_name'];
			$i++;
		}

		if ($paginate) {
			$sqlQuery = 'select count(products_options_id) from ' . TABLE_PRODUCTS_OPTIONS . ' where language_id=' . $languageId;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt = (int)$row['cnt'];
			}

			$value{$handle . 'MetaData'}{'max_rows'}= (int)$max_rows;
			$value{$handle . 'MetaData'}{'offset'}= $offset;
			$value{$handle . 'MetaData'}{'page_links_max'} = (int)MAX_DISPLAY_PAGE_LINKS;
			$value{$handle . 'MetaData'}{'rows'}= $i;
			$value{$handle . 'MetaData'}{'total_rows'}= $cnt;
		}

		return $value;
	}

	function edit(&$params) 
	{
		$languageId = (int)$params['languageId'];

		$i = 0;
		while (true)
		{
			$i++;
			if ($i > 1000) break;

			$productOptionId = (int)$params['productOptionId-'.$i];
			if ($productOptionId == 0) break;

			$sqlQuery = 'select products_options_id from ' . TABLE_PRODUCTS_OPTIONS . ' where products_options_id=' . $productOptionId . ' and language_id=' . $languageId;
			$dataReturned = mysql_query($sqlQuery) or die(mysql_error());
			$cnt = 0;
			while($row = mysql_fetch_array($dataReturned))
			{
				$cnt++;
			}

			$productOptionName = prepare_input($params['productOptionName-'.$i]);
			if ($productOptionName == null || strlen($productOptionName) == 0) continue;

			$deleteRow = (boolean)$params['productOptionDelete-'.$i];

			if ($deleteRow) {
				if ($productOptionId < 1) continue;
				$sql = 'delete from ' . TABLE_PRODUCTS_OPTIONS . ' where products_options_id=' . $productOptionId;
				mysql_query($sql) or die(mysql_error());
			}
			else {
				$sql_data_array = array();
				$sqlAction = 'insert';
				$where = '';
				if ($cnt > 0) {
					$sqlAction = 'update';
					$where = 'products_options_id=' . $productOptionId . ' and language_id=' . $languageId;
				}
				else {
					$sql_data_array['products_options_id'] = $productOptionId;
					$sql_data_array['language_id'] = $languageId;
				}
				$sql_data_array['products_options_name'] = $productOptionName;
				db_perform(TABLE_PRODUCTS_OPTIONS, $sql_data_array, $sqlAction, $where);
			}
		}

		return $this->query($params);
	}
}
?>
